/* 
 * File:   ebook.h
 * Author: Luis Aguirre
 * Description: Represent an ebook
 *
 * Created on jule 16, 2013, 04:41 PM
 */

#ifndef EBOOK_H
#define	EBOOK_H

#include "ebookpage.h"
#include "backbookindex.h"
#include "dictionary.h"
#include "../exceptions/fileexception.h"
#include "../exceptions/filenotfoundexception.h"
#include "../exceptions/openfileexception.h"

class EBook
{
public:
    
    /**
     * Constructor that load the pages of the book.
     * 
     * @param eBookPath Path of the ebook
     */
    EBook(const char* eBookPath, const char* dictionaryPath);
    
    /**
     * Add a new page in the ebook
     * 
     * @param page New page that will be added
     * 
     * @return True if the page was added
     */
    bool addPage(const EBookPage& page);
    
    /**
     * Return the list of the pages.
     * 
     * @return The list of the Pages
     */
    std::list<EBookPage>& getPages()
    {
        return pages_;
    }
    
    /**
     * Show the ebook in console.
     */
    void printBook()
    {
        std::list<EBookPage>::iterator pagesIt = pages_.begin();
        for (; pagesIt != pages_.end(); pagesIt++) 
        {
            printf("Pg: %d\n", (*pagesIt).number);
            (*pagesIt).printPage();
            printf("\n");
        }
    }
    
    /**
     * Return the back book index.
     * 
     * @return The back book index.
     */
    BackBookIndex getBackBockIndex()
    {
        return index_;
    }
    
    /**
     * Generate the back boock index of the ebook given the path of the words list .
     * 
     * @param wordsPath Path of the words
     * 
     * @return The Back book index
     */
    BackBookIndex generateBackBockIndex(Dictionary dictionary);
    
    
    
private:
    
    /**
     * Title of the ebook.
     */
    std::string title_;
    
    /**
     * Pages of the ebook.
     */
    std::list<EBookPage> pages_;
    
    /**
     * Dictionary.
     */
    Dictionary* dictionary_;
    
    /**
     * Back book index.
     */
    BackBookIndex index_;
    
    /**
     * Read the ebook and load the pages.
     * 
     * @param eBookPath Ebook path
     */
    void readEBook(const char* eBookPath) throw (FileException);
    
    void findWordsToIndex(EBookPage& page);
    
    
};

void trimLine(std::string& line);

#endif	/* EBOOK_H */

